<template>
  <div class="layerd-now" ref="layerd_now_ref"></div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      allData: [], /// 服务端数据
      layerdNowChart: null, // 分层健康度图表实例
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    // 监听屏幕大小变化事件
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  created() {},
  methods: {
    // 初始化图表
    initChart() {
      this.layerdNowChart = this.$echarts.init(this.$refs.layerd_now_ref);
      var option = {
        backgroundColor: "#fff",
        color: ["rgba(19, 173, 255, 1)"],
        title: {
          show: true,
          text: "分层健康度",
          left: "5%",
          top: "1%",
          textStyle: {
            // color: "#fff",
            fontStyle: "normal",
            fontWeight: "normal",
          },
        },
        toolbox: {
          show: true,
          orient: "vertical",
          top: "10%",
          feature: {
            mark: {
              show: true,
            },
            dataView: {
              show: true,
              readOnly: false,
            },
            saveAsImage: {
              show: true,
            },
          },
        },
        // 鼠标悬浮提示
        tooltip: {
          show: true,
          trigger: "item",
        },
        radar: {
          center: ["50%", "50%"],
          radius: "70%",
          startAngle: 90,
          splitNumber: 4,
          shape: "polygon", // circle: 原型
          splitArea: {
            areaStyle: {
              color: ["transparent"],
            },
          },
          // 一圈的字
          axisLine: {
            show: true,
            lineStyle: {
              color: "#000", //
            },
          },
          // 圆圈线的颜色
          splitLine: {
            show: true,
            lineStyle: {
              color: "#000", //
            },
          },
        },
        series: [
          {
            name: "分层健康度",
            type: "radar",
            symbol: "circle",
            symbolSize: 10,
            itemStyle: {
              normal: {
                color: "rgba(19, 173, 255, 1)",
                borderColor: "rgba(19, 173, 255, 0.4)",
                borderWidth: 10,
              },
            },
            areaStyle: {
              normal: {
                color: "rgba(19, 173, 255, 0.5)",
              },
            },
            lineStyle: {
              normal: {
                color: "rgba(19, 173, 255, 1)",
                width: 1,
                type: "dashed",
              },
            },
          },
        ],
      };
      this.layerdNowChart.setOption(option);
    },
    // 获取服务端数据
    async getData() {
      const { data: ret } = await this.$axios.get("layerd_now_data");
      this.allData = ret.data;
      this.updateChart();
    },
    // 更新图表
    updateChart() {
      // 雷达图的分类
      const indicators = this.allData.map((item) => {
        return {
          name: item.name,
          max: 100,
        };
      });
      const values = this.allData.map((item) => {
        return item.score;
      });
      var option = {
        radar: {
          indicator: indicators,
        },
        series: [
          {
            data: [values],
          },
        ],
      };
      this.layerdNowChart.setOption(option);
    },
    // 屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.layerd_now_ref.offsetWidth / 100) * 2.6;
      var option = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        // radar: {
        // },
      };
      this.layerdNowChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.layerd-now {
  width: 100%;
  height: 100%;
}
</style>